package appfuse.framework.util;

import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import appfuse.framework.exceptions.SendEmailException;

/**
 * 邮件发送通用类
 * <p>Title:</p>
 * <p>Description:</p>
 * <p>Copyright: Copyright (c) 2005</p>
 * <p>Company: </p>
 * @author savage
 * @since 2005-11-11
 */
public class SendEmail {
	private String msgText = null; //邮件内容
	private String subject = null; //邮件标题
	private String smtpHost = "smtp.gmail.com"; //发送邮件的SMTP服务器
	private String from = null; //邮件发送者
	private String to = null; //邮件接受者
	//String user = "chenjiaming"; // 邮件服务器登录用户名
	//String password = "king+5688"; // 邮件服务器登录密码
	String user = "appfuse"; // 邮件服务器登录用户名
	String password = "3335688"; // 邮件服务器登录密码
	/**
	 * @param from 要设置的 from。
	 */
	public void setFrom(String from) {
		this.from = from;
	}

	/**
	 * @param msgText 要设置的 msgText。
	 */
	public void setMsgText(String msgText) {
		this.msgText = msgText;
	}

	/**
	 * @param smtpHost 要设置的 smtpHost。
	 */
	public void setSmtpHost(String smtpHost) {
		this.smtpHost = smtpHost;
	}

	/**
	 * @param subject 要设置的 subject。
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}

	/**
	 * @param to 要设置的 to。
	 */
	public void setTo(String to) {
		this.to = to;
	}

	public SendEmail(){}
	
	/**
	 * 邮件发送
	 * @throws Exception
	 */
	public void sendnow() throws Exception{
		try{
			Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
			final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
			// Get a Properties object
			Properties props = System.getProperties();
			props.setProperty("mail.smtp.host", smtpHost);
			props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
			props.setProperty("mail.smtp.socketFactory.fallback", "false");
			props.setProperty("mail.smtp.port", "465");
			props.setProperty("mail.smtp.socketFactory.port", "465");
			props.put("mail.smtp.auth", "true");

			
			Session session = Session.getDefaultInstance(props, new Authenticator(){
				protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(user, password);
				}});
	
			MimeMessage message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			InternetAddress[] address = {new InternetAddress(to)};
			message.setRecipients(Message.RecipientType.TO, address);
			sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder(); 
			message.setSubject("=?GB2312?B?"+enc.encode(subject.getBytes())+"?=");
			message.setSentDate(new Date());
			message.setText(msgText, "gb2312");
			
			Transport transport = session.getTransport("smtp");
			transport.connect(smtpHost, user, password);
			transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
			transport.close();
			//Transport.send(message);
			
		}catch(Exception ex){
			ex.printStackTrace();
			throw new SendEmailException();
		}
	}
	
	/**
	 * 发送HTML邮件
	 * @throws Exception
	 */
	public void sendnowHtml() throws Exception {
		//TODO:hewei
	}
}
